import { ConfigEnv, UserConfig, defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { wrapperEnv } from "./build/getEnv";
import { createProxy } from "./build/proxy";
import Components from 'unplugin-vue-components/vite'
import Unocss from 'unocss/vite'
import AutoImport from "unplugin-auto-import/vite"

// https://vitejs.dev/config/
export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
  const root = process.cwd();

  const env = loadEnv(mode, root);
  const viteEnv = wrapperEnv(env);

  return {
    plugins: [vue(), Unocss(), Components({

      // 指定组件所在文件夹的位置，默认是src/components
      dirs: ['src/components'],
      extensions: ['vue'],//文件扩展
      // 配置type文件生成位置
      dts: 'src/components.d.ts'
    }),
    AutoImport({
      imports: ["vue", "vue-router", "pinia"], //自动引入vue的ref、toRefs、onmounted等，无需在页面中再次引入
    })],

    resolve: {
      // 设置文件./src路径为 @
      alias: {
        "@": "/src",
        "#": "/src/store",

      },
    },
    server: {
      host: "0.0.0.0",
      port: viteEnv.VITE_PORT,
      open: viteEnv.VITE_OPEN,
      cors: true,
      // Load proxy configuration from .env.development
      proxy: createProxy(viteEnv.VITE_PROXY)
    },
  }
})
